

class Parser:

    index = 0
    command = ''
    length = 0

    def __init__(self, command: str):
        self.command = command
        self.length = len(command)

    def simple_parse(self):
        is_quote = self.start_with("'") or self.start_with('"')
        quote = None
        is_espace = False

        if is_quote:
            quote = self.consume_char()

        def process(ch: str):
            nonlocal is_espace
            if is_espace == True:
                is_espace = False
                return True

            if ch == ' ' and (not is_quote):
                return False

            if ch == '\\':
                is_espace = True
                return True

            if is_quote and quote == ch:
                self.consume_char()
                return False
            return True
        return self.consume_while(process)

    def parse(self):
        result = []
        while(True):
            self.consume_whitespace()
            if self.is_eof():
                break
            result.append(self.simple_parse())
        return result

    def is_eof(self) -> bool:
        return not self.index < self.length

    def consume_char(self):
        ch = self.command[self.index]
        self.index += 1
        return ch

    def next_char(self) -> str:
        return self.command[self.index]

    def start_with(self, s: str) -> bool:
        return self.command[self.index:].startswith(s)

    def consume_whitespace(self):
        self.consume_while(lambda ch: ch.isspace())

    def consume_while(self, callback: callable(str)):
        result = ""
        while (not self.is_eof()) and callback(self.next_char()):
            result += self.consume_char()
        return result


if __name__ == "__main__":
    parser = Parser("'ab \\' abc bcd' -f a23 abc")
    print(parser.parse(), ["ab \\' abc bcd", '-f', 'a23', 'abc'])
    parser = Parser("abc 'aa cc'")
    print(parser.parse(), ['abc', 'aa cc'])
